﻿using AntdUI;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AttendanceSysytem.Controls
{
    public static  class AntduiExt
    {
        public static void PaintBadge(this IControl control, DateBadgeEx badge, Font font, Rectangle rect, Canvas g)
        {
            Color color = badge.Fill ?? control.BadgeBack ?? Colour.Error.Get("Badge");
            float dpi = Config.Dpi;
            if (badge.BageText.Length == 0)
            {
                RectangleF rect2 = new RectangleF((float)rect.Right - 10f, (float)rect.Top + 2f, 8f, 8f);
                g.FillEllipse(color, rect2);
                g.DrawEllipse(color, dpi, rect2);
                return;
            }

            using StringFormat format = SF_NoWrap();
            string text = badge.BageText;
            Size size = g.MeasureString(text, font);
            int num = (int)((float)size.Height * 1.2f);
            if (size.Height > size.Width)
            {
                Rectangle rectangle = new Rectangle(rect.Right - num + 6, rect.Top - 8, num, num);
                g.FillEllipse(color, rectangle);
                g.DrawEllipse(Colour.ErrorColor.Get("Badge"), dpi, rectangle);
                g.String(text, font, Colour.ErrorColor.Get("Badge"), rectangle, format);
                return;
            }

            int num2 = size.Width + (num - size.Height);
            Rectangle rect3 = new Rectangle(rect.Right - num2 + 6, rect.Top - 8, num2, num);
            using (GraphicsPath path = rect3.RoundPath(rect3.Height))
            {
                g.Fill(color, path);
                g.Draw(Colour.ErrorColor.Get("Badge"), dpi, path);
            }

            g.String(text, font, Colour.ErrorColor.Get("Badge"), rect3, format);
        }

        //
        // 摘要:
        //     文本布局（不换行）
        //
        // 参数:
        //   tb:
        //     垂直（上下）
        //
        //   lr:
        //     水平（前后）
        public static StringFormat SF_NoWrap(StringAlignment tb = StringAlignment.Center, StringAlignment lr = StringAlignment.Center)
        {
            StringFormat obj = new StringFormat(StringFormat.GenericTypographic)
            {
                LineAlignment = tb,
                Alignment = lr
            };
            obj.FormatFlags |= StringFormatFlags.NoWrap;
            return obj;
        }

    }
}
